package com.springcloud.microservice.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;

/**
 * Author : 周世雄 <br>
 * Date : 2018/11/19 10:21 <br>
 * E-mail : zhoushixiong@chinasoftinc.com
 * Description :
 */
public class PreRequestLogFilter extends ZuulFilter {

    private static final Logger LOGGER = LoggerFactory.getLogger(PreRequestLogFilter.class);

    /**
     * 返回过滤器的类型。有pre、route、post、error几种
     * @return 过滤器的类型
     */
    @Override
    public String filterType() {
        return "pre";
    }

    /**
     * 返回指定过滤器的执行顺序，不同过滤器允许返回相同的数字
     * @return 过滤器的执行顺序
     */
    @Override
    public int filterOrder() {
        return 1;
    }

    /**
     * 返回过滤器是否执行
     * @return 过滤器是否执行
     */
    @Override
    public boolean shouldFilter() {
        return true;
    }

    /**
     * 过滤器具体逻辑
     * @return
     */
    @Override
    public Object run() {
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();
        LOGGER.info(String.format("send %s request to %s" , request.getMethod() , request.getRequestURI().toString()));
        return null;
    }
}
